package com.tddx.activiti.business.index.controller;

import java.io.Serializable;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.ServletContextResource;

import com.tddx.activiti.base.BaseController;
import com.tddx.activiti.base.BaseEntity;
import com.tddx.activiti.business.sys.dao.systuser.SysTuserDao;
import com.tddx.activiti.business.sys.entity.systuser.SysTuserEntity;
import com.tddx.activiti.utils.ConnectionUtil;
import com.tddx.activiti.utils.SpringContextHolder;
import com.tddx.activiti.utils.ftl.FtlUtil;
import com.tddx.activiti.utils.ftl.data.BaseData;
import com.tddx.activiti.utils.springutils.CustomizedPropertyConfigurer;

@Controller
@RequestMapping("main")
public class MainCtrl extends BaseController<BaseEntity, Serializable>{

	String sql = "ceshi";

	@Value("#{mainCtrl.sql}")
	private String sql2;

	@RequestMapping("prop")
	@ResponseBody
	public Object prop(String str) {
		System.out.println(CustomizedPropertyConfigurer.getCtxProp("VC_SERVER"));
		return sql2;
	}
	
	/*@Autowired JmsTemplate jms;
	
	@RequestMapping("jmstest")
	@ResponseBody
	public Object jmstest(String str) {
		jms.convertAndSend(str);
		return sql2;
	}*/
	
	@RequestMapping("autodm")
	@ResponseBody
	public Object autodm(@RequestParam String tableName, @RequestParam(defaultValue="false") Boolean tree) {
		BaseData data = BaseData.getInstance(tableName, tree);
		FtlUtil.makeControllerFtl(data);
		FtlUtil.makeServiceFtl(data);
		FtlUtil.makeJdbcFtl(data);
		FtlUtil.makeDaoFtl(data);
		FtlUtil.makeEntityFtl(data);
		FtlUtil.makeInputFtl(data);
		FtlUtil.makeResultFtl(data);
		return "success";
	}

	@Autowired private ConnectionUtil conutil;
	
	@RequestMapping("test2")
	@ResponseBody
	public Object test2(@RequestParam Map<String, Object> map) throws Exception {
//	oracle	元数据操作
		DatabaseMetaData dmd = conutil.getDmd();
		ResultSet rs = dmd.getColumns(null, "ZHAO", "SYS_T_USER", null);
		while(rs.next()){
			System.out.println(rs.getString("COLUMN_NAME"));
		}
		
		ServletContextResource scr = new ServletContextResource(
				ContextLoader.getCurrentWebApplicationContext()
					.getServletContext(), "test.txt");
		System.out.println(scr.getPath());
		ContextLoader.getCurrentWebApplicationContext().getServletContext();
		return sql2;
		
	}
	
	@RequestMapping("test3{jsp}")
	public String test3(@PathVariable String jsp){
		SpringContextHolder.getBean(SysTuserDao.class).findByVcUsername("zhao");
		return "modules/sys/sysTrolemenuInput";
	}
	
	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

}
